package com.bjsxt.shop.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.dao.IAGoodsBrandDao;
import com.bjsxt.shop.goods.dao.IAGoodsCateDao;
import com.bjsxt.shop.goods.dao.IAGoodsDao;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService
{
	@Resource
	private IAGoodsBrandDao goodsBrandDao;
	@Resource
	private IAGoodsCateDao goodsCateDao;
	@Resource
	private IAGoodsDao goodsDao;
	
	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.save(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		/* 保存品牌 */
		JSONObject resultJSON = this.saveOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.update(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.updateOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoodsBrand> roleList= this.goodsBrandDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	
	@Override
	public AGoodsCate findOneGoodsCateService(Map<String, Object> condMap)
	{
		return this.goodsCateDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.save(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.update(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public List<AGoodsCate> findCondListGoodsCateService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsCate> roleList = this.goodsCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return roleList;
		}
		/* 不分页查询数据 */
		return this.goodsCateDao.findList(condMap);
	}
	
	@Override
	public AGoods findOneGoodsService(Map<String, Object> condMap)
	{
		return this.goodsDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.save(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	/**
	 * 保存一条商品
	 * @param goods	保存的对象
	 * @return	json:{"code":"0","info":"成功","data":{"effect":"","id":""}}
	 */
	public JSONObject saveOneGoodsService(AGoods goods,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.saveOneGoodsService(goods);
		return resultJSON ; 
	}
	
	@Override
	public JSONObject updateOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.update(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String, Object> findCondListGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<AGoods> goodsList = this.goodsDao.findList(condMap);
			resultMap.put("list", goodsList);
			
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			/* 设置总记录数 */
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap ; 
		}
		/* 不分页查询数据 */
		List<AGoods> goodsList = this.goodsDao.findList(condMap);
		resultMap.put("list", goodsList);
		return resultMap ; 
	}
}
